package com.zone.editor;

import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.zone.editor.inter.DrawingView;
import com.zone.editor.inter.EditorViewState;
import com.zone.editor.inter.OnEditorListener;
import com.zone.editor.photoEditor.R;

public class GraphicManager {
    private final ViewGroup mViewGroup;
    private final EditorViewState mViewState;
    private OnEditorListener mOnEditorListener;
    private ImageView imgPhotoEditorClose;

    public GraphicManager(ViewGroup parentView, EditorViewState viewState) {
        this.mViewGroup = parentView;
        mViewState = viewState;
    }

    public void addView(Graphic graphic) {
        View view = graphic.getRootView();
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        int parentHeight = mViewGroup.getHeight();
        int parentWidth = mViewGroup.getWidth();
        int width = View.MeasureSpec.makeMeasureSpec(0 ,View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0 ,View.MeasureSpec.UNSPECIFIED);
        view.measure(width,height);
        view.setX((float)((parentWidth)/2));
        view.setY((float)((parentHeight)/2));
        mViewGroup.addView(view, params);

        mViewState.addAddedView(view);
        if (mOnEditorListener != null)
            mOnEditorListener.onAddViewListener(graphic.getViewType(), mViewState.getAddedViewsCount());
    }

    public void removeView(Graphic graphic) {
        View view = graphic.getRootView();
        if (mViewState.containsAddedView(view)) {
//            parentView.removeView(view);
            mViewGroup.removeView(view);
            mViewState.removeAddedView(view);
            mViewState.pushRedoView(view);
            if (mOnEditorListener != null) {
                mOnEditorListener.onRemoveViewListener(
                        graphic.getViewType(),
                        mViewState.getAddedViewsCount()
                );
            }
        }
    }

    public void updateView(View view) {
//        parentView.updateViewLayout(view, view.getLayoutParams());
        mViewGroup.updateViewLayout(view, view.getLayoutParams());
        mViewState.replaceAddedView(view);
    }

    public void setOnEditorListener(OnEditorListener onEditorListener) {
        mOnEditorListener = onEditorListener;
    }

    @Nullable
    OnEditorListener getOnEditorListener() {
        return mOnEditorListener;
    }

    public boolean undoView() {
        if (mViewState.getAddedViewsCount() > 0) {
            View removeView = mViewState.getAddedView(
                    mViewState.getAddedViewsCount() - 1
            );
            if (removeView instanceof DrawingView) {
                DrawingView drawingView = (DrawingView) removeView;
                return drawingView.undo();
            }else {
                mViewState.removeAddedView(mViewState.getAddedViewsCount() - 1);
//                parentView.removeView(removeView);
                mViewGroup.removeView(removeView);
                mViewState.pushRedoView(removeView);
            }
            if (mOnEditorListener != null) {
                Object viewTag = removeView.getTag();
                if (viewTag instanceof ViewType) {
                    mOnEditorListener.onRemoveViewListener(
                            (ViewType) viewTag,
                            mViewState.getAddedViewsCount()
                    );
                }
            }
        }
        return mViewState.getAddedViewsCount() != 0;
    }

    public boolean redoView() {
        if (mViewState.getRedoViewsCount() > 0) {
            View redoView = mViewState.getRedoView(
                    mViewState.getRedoViewsCount() - 1
            );
            if (redoView instanceof DrawingView) {
                DrawingView drawingView = (DrawingView) redoView;
                return drawingView.redo();
            }else {
                mViewState.popRedoView();
//                parentView.addView(redoView);
                mViewGroup.addView(redoView);
                mViewState.addAddedView(redoView);
            }
            Object viewTag = redoView.getTag();
            if (mOnEditorListener != null && viewTag instanceof ViewType) {
                mOnEditorListener.onAddViewListener(
                        (ViewType) viewTag,
                        mViewState.getAddedViewsCount()
                );
            }
        }
        return mViewState.getRedoViewsCount() != 0;
    }
}
